package com.dhcc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.base.BaseServiceImpl;
import com.dhcc.entity.SysUser;
import com.dhcc.httpApiService.IdGeneratorService;
import com.dhcc.mapper.SysUserMapper;
import com.dhcc.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户表接口实现类
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.service.impl
 * @Title SysUserServiceImpl
 * @Date 2024/11/19 下午5:51
 * @Author LiuGuoting
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    /**
     * id生成器接口
     */
    @Resource
    private IdGeneratorService idGeneratorService;
    /**
     * 自定义分页查询
     *
     * @param page 分页对象
     * @param sysUser 用户信息对象实体
     * @return IPage<SysUser> 分页对象
     */
    @Override
    public IPage<SysUser> selectUserPage(IPage<SysUser> page, SysUser sysUser) {
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询未被删除的用户信息
        sysUserLambdaQueryWrapper.eq(SysUser::getDelFlag, 0);
        // 判断条件查询用户名是否为空，不为空则进行模糊查询
        sysUserLambdaQueryWrapper.like(StringUtils.hasText(sysUser.getUserName()),SysUser::getUserName, sysUser.getUserName());
        // 执行查询
        List<SysUser> sysUsers = baseMapper.selectList(page, sysUserLambdaQueryWrapper);
        // 重新设置创建用户的名称和修改用户的名称
        for (SysUser user : sysUsers) {
            user.setCreateUser(getUserNameById(user.getCreateUser()));
            user.setUpdateUser(getUserNameById(user.getUpdateUser()));
        }
        return page.setRecords(sysUsers);
    }

    /**
     * 新增用户信息
     *
     * @param sysUser 用户信息对象实体
     * @return boolean
     */
    @Override
    public Boolean insertUser(SysUser sysUser) {
        sysUser.setId(idGeneratorService.nextId().toString());
        sysUser.setLoginDate(null);
        return baseMapper.insert(sysUser) > 0;
    }

    /**
     * 编辑用户信息
     * @param sysUser 用户信息对象实体
     * @return boolean
     */
    @Override
    public Boolean updateUser(SysUser sysUser) {
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUser::getId, sysUser.getId());
        return baseMapper.update(sysUser, updateWrapper) > 0;
    }

    /**
     * 更新用户状态
     *
     * @param sysUser 用户信息对象实体
     * @return boolean
     */
    @Override
    public Boolean updateStatus(SysUser sysUser) {
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUser::getId, sysUser.getId());
        updateWrapper.set(SysUser::getStatus, sysUser.getStatus());
        return baseMapper.update(updateWrapper) > 0;
    }

    /**
     * 更新批量用户状态
     *
     * @param ids 用户ID列表
     * @param newStatus 新状态
     * @return boolean
     */
    @Override
    public Boolean updateBatchStatus(List<String> ids, int newStatus) {
        // 1. 参数校验
        if (ids == null || ids.isEmpty() || (newStatus != 0 && newStatus != 1)) {
            return false;
        }
        // 2. 构造更新条件
        LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SysUser::getId, ids);
        updateWrapper.set(SysUser::getStatus, newStatus);
        // 3. 执行更新操作
        // 检查更新是否成功，即更新的记录数大于零
        return baseMapper.update(updateWrapper)> 0;
    }

    /**
     * 根据用户ID获取用户名
     *
     * @param id 用户ID
     * @return 用户名，如果用户不存在则返回null
     */
    @Override
    public String getUserNameById(String id) {
        SysUser sysUser = baseMapper.selectById(id);
        if (sysUser == null) {
            return null;
        }
        return sysUser.getUserName();
    }


}